package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityDaliyReport;
import com.loongtech.core.jpa.manager.ManagerBase;
import com.loongtech.core.util.ScheduleUtils;
import com.loongtech.core.util.Time;
import com.loongtech.core.util.Utils;
import org.quartz.Scheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.loongtech.bi.manager.system.SysProjectManager.projectIdNameMap;

@Service
public class DailyReportManager extends ManagerBase<EntityDaliyReport> {

    @Resource
    private Scheduler scheduler;

    private static final long serialVersionUID = 1L;

    /**
     * 系统启动时执行每日报表任务
     */
    public void addAllEntityDaliyReport() {
        long now = Time.currentTime();
        long today8 = Time.getBeginningOfDay(now) + 8 * Time.HOUR;  // 今天8点
        if (now >= today8) {
            ScheduleUtils.createDailyReportScheduleJob(scheduler, "dailyReport", new Date(today8 + Time.DAY), new Date(Time.currentTime() + 100 * Time.DAY), 24, Integer.MAX_VALUE);
        } else {
            ScheduleUtils.createDailyReportScheduleJob(scheduler, "dailyReport", new Date(today8), new Date(Time.currentTime() + 100 * Time.DAY), 24, Integer.MAX_VALUE);
        }
    }


    /**
     * 增加
     */
    public void addEntityDaliyReport(EntityDaliyReport e) {
        persist(e);
    }


    /**
     * 删除
     */
    public void delEntityDaliyReport(Integer id) {
        removeById(id);
    }


    /**
     * 修改
     */
    public void modifyEntityDaliyReport(EntityDaliyReport e) {
        merge(e);
    }

    /**
     * 获取所有列表
     */
    public List<EntityDaliyReport> queryList() {
        return findByQuery("from EntityDaliyReport order by id");
    }

    /**
     * 查询任务
     */
    public List<EntityDaliyReport> searchByCriteria(EntityDaliyReport e) {
        List<Object> list = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityDaliyReport e where 1=1 ");

        if (e.getThreshHold() != null) {
            hql.append(" and e.threshHold = ?");
            list.add(e.getThreshHold());
        }
        if (!Utils.isEmptyStr(e.getDescription())) {
            hql.append(" and e.description like ?");
            list.add("%" + e.getDescription() + "%");
        }
        if (!Utils.isEmptyStr(e.getGameName())) {
            hql.append(" and e.gameName like ?");
            list.add("%" + e.getGameName() + "%");
        }
        if (!Utils.isEmptyStr(e.getInquerySQLOne())) {
            hql.append(" and e.inquerySQLOne like ?");
            list.add("%" + e.getInquerySQLOne() + "%");
        }
        if (!Utils.isEmptyStr(e.getInquerySQLTwo())) {
            hql.append(" and e.inquerySQLTwo like ?");
            list.add("%" + e.getInquerySQLTwo() + "%");
        }
        hql.append(" order by e.creatTime");
        return findByQuery(hql.toString(), list.toArray());
    }

    /**
     * 查询任务(springmvc)
     */
    public Page<List<EntityDaliyReport>> queryByCriteria(Integer curPage, Integer pageSize, EntityDaliyReport e) {
        List<Object> param = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityDaliyReport e where 1=1 ");

        if (e.getThreshHold() != null) {
            hql.append(" and e.threshHold = ?");
            param.add(e.getThreshHold());
        }
        if (!Utils.isEmptyStr(e.getDescription())) {
            hql.append(" and e.description like ?");
            param.add("%" + e.getDescription() + "%");
        }
        if (e.getGameId() != null) {
            hql.append(" and e.gameId like ?");
            param.add("%" + e.getGameId() + "%");
        }
        if (!Utils.isEmptyStr(e.getInquerySQLOne())) {
            hql.append(" and e.inquerySQLOne like ?");
            param.add("%" + e.getInquerySQLOne() + "%");
        }
        if (!Utils.isEmptyStr(e.getInquerySQLTwo())) {
            hql.append(" and e.inquerySQLTwo like ?");
            param.add("%" + e.getInquerySQLTwo() + "%");
        }
        hql.append(" order by e.creatTime");
        Integer totalSize = countNum(hql.toString(), param.toArray());
        //数据
        List<EntityDaliyReport> list = findByQuery((curPage - 1) * pageSize, pageSize, hql.toString(), param.toArray());
        if (list != null && list.size() > 0) {
            for (EntityDaliyReport daliyReport : list) {
                String gameid = daliyReport.getGameId();
                for (Map.Entry<String, String> entry : projectIdNameMap.entrySet()) {
                    gameid = gameid.replace(entry.getKey() + ",", entry.getValue() + ",");
                }
                daliyReport.setGameName(gameid);
            }
        }
        return new Page<>(list, totalSize, curPage, pageSize);
    }
}
